﻿using Newtonsoft.Json;
using PLCSharp.CoreModel.Prism;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.IO;

namespace PLCSharp.Views.MainWindow
{
    public class SystemConfigViewModel : DialogAwareBase
    {
        public SystemConfigViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
        {
            for (int i = 0; i < 10; i++)
            {
                EnvironmentVariables.Add($"EnvironmentVariable{i}");
            }
            LoadConfig();
        }

        public ObservableCollection<string> EnvironmentVariables { get; set; } = [];

        private void LoadConfig()
        {

            if (!Directory.Exists("./Config/"))
            {
                Directory.CreateDirectory("./Config/");
            }

            if (File.Exists("./Config/EnvironmentVariable.json"))
            {
                var json = File.ReadAllText("./Config/EnvironmentVariable.json");
                dynamic config = JsonConvert.DeserializeObject(json);
                int i = 0;
                foreach (var item in config.EnvironmentVariables)
                {
                    EnvironmentVariables[i] = item;
                    i++;
                }
            }


        }
 
        private void SaveConfig()
        {

            if (!Directory.Exists("./Config/"))
            {
                Directory.CreateDirectory("./Config/");
            }

            dynamic config = new { EnvironmentVariables };

            var json = JsonConvert.SerializeObject(config);

            File.WriteAllText("./Config/EnvironmentVariable.json", json);

        }
        private DelegateCommand<string> _SetPath;
        public DelegateCommand<string> SetPath =>
            _SetPath ??= new DelegateCommand<string>(ExecuteSetPath);

        void ExecuteSetPath(string param)
        {
            try
            {


                Microsoft.Win32.OpenFileDialog openFileDialog = new()
                {
                    Title = "选择文件夹",
                    Filter = "文件夹|*.directory",
                    FileName = "选择此文件夹",

                    ValidateNames = false,
                    CheckFileExists = false,
                    CheckPathExists = true,
                    Multiselect = true,//允许同时选择多个文件
                    InitialDirectory = "D:"//指定启动路径
                };
                if (openFileDialog.ShowDialog() == true)

                {
                    var path = openFileDialog.FileName.Replace("选择此文件夹.directory", "");

                    if (!System.IO.Directory.Exists(path))
                    {
                        System.Windows.MessageBox.Show(path + "文件夹不存在", "选择文件提示");
                        return;
                    }


                    EnvironmentVariables[int.Parse(param)] = path;
                    SaveConfig();
                }
            }
            catch (Exception)
            {

            }
        }


    }
}
